#!/usr/bin/python
#encoding=utf-8

import re
import time
import simplejson
import urllib2,StringIO,zlib,gzip
import requests
import uuid
from wm.etc.config import SOCKET
from wm.lib import monitorlib
from wm.lib.tam import tamper
from wm.etc.config import CLUSTER,ALARMTYPE
from wm.lib.loglib import log_error
from wm.db.TransactionManager import TransactionManager

def decode (page):
    encoding = page.info().get("Content-Encoding")    
    if encoding in ('gzip', 'x-gzip', 'deflate'):
        content = page.read()
        if encoding == 'deflate':
            data = StringIO.StringIO(zlib.decompress(content))
        else:
            data = gzip.GzipFile('', 'rb', 9, StringIO.StringIO(content))
        page = data.read()
    return page

def get_page_content(url):
    opener = urllib2.build_opener()
    opener.addheaders = [('Referer', 'referer'),('User-Agent', 'uagent'),('Accept-Encoding', 'gzip,deflate')]
    usock = opener.open(url)
    url = usock.geturl()
    data = decode(usock)
    usock.close() 
    return data  

def page_tamper_check(url, websiteid, websitename):
    taglist = { "script":0,"iframe":0,"frameset":0,"a":0}
    try:
        response = requests.get(url)
        pattern = re.compile(r"<[a-zA-Z]* ")
        alltags = pattern.findall(response.content)
        for item in alltags:
            t = item[1:len(item)-1]
            if t in taglist.keys():
                taglist[t] += 1        
        tagjson = simplejson.dumps(taglist)

        tran = TransactionManager()
        try:
            sql = "select * from featuretags where websiteConfigInfo_id = '"+websiteid+"'"
            featuretags = tran.select(sql)
            if featuretags:
                tagdic = simplejson.loads(featuretags[0][3])
                for key,value in featuretags:
                    if tagdic[key] != value:
                        sql = "update featuretags set modifyDate=CURRENT_TIMESTAMP(),tagData='"+tagjson+"'" + " where id ='" +featuretags[0][0]+"'"
                        tran.update(sql)
                        
                        alarmcontent = websitename+"存在疑似篡改标签："+tagdic[key]
                        alarmid = uuid.uuid1().__str__().replace("-","") 
                        alarmcontent = alarmcontent.decode('utf-8')
                        sql = "insert into alarmevent(id,createDate,modifyDate,alertCategory,alertContent,occurrenceDate,status,websiteName,websiteConfigInfo_id) "+\
                                        "values('" + alarmid + "'," + " CURRENT_TIMESTAMP()," + " CURRENT_TIMESTAMP()," +\
                                        str(ALARMTYPE['PAGE_TAMPER_ALARM']) + ",'"+ alarmcontent + "'," + "CURRENT_TIMESTAMP()," + str(0) + ",'" + websitename + "','"+ websiteid + "')"
                        tran.execute(sql)
                        
                        sql = "insert into pagetamper(id,createDate,modifyDate,isTampered,tamperContent,websiteConfigInfo_id,alarmEvent_id) values(replace(uuid(),'-',''),CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),1,'"+alarmcontent+"','"+websiteid+"','"+alarmid+"')"
                        tran.execute(sql)
                        
                        monitorlib.update_recent_alarmevent(tran,row[0],row[2],str(ALARMTYPE['PAGE_TAMPER_ALARM']),alarmcontent)
                        break        
            else:
                sql = "insert featuretags(id,createDate,modifyDate,tagData,websiteConfigInfo_id) values(replace(uuid(),'-',''),CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP(),'" + tagjson + "','"+websiteid+"')"     
                tran.execute(sql)
                
            tran.commitTransaction()
        except:
            tran.rollbackTransaction()
        finally:
            tran.close()
    except Exception as e:
        log_error("page_tamper_check():"+str(e))
        time.sleep(SOCKET['INTERVAL'])

def page_tamper_monitor(pa):
    #tamper.tam.check_tamper(pa)
    pass
            

if __name__=="__main__":
    sitelist = monitorlib.get_website_list(1)
    if sitelist:
        for row in sitelist:
            page_tamper_check(row[1],row[0],row[2])